#if 0
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <linux/can.h>
#include <sys/ioctl.h>
#include <linux/can/raw.h>


#define CAN_SET_FILTER CAN_RAW_SET_FILTER




#define CAN_DEVICE "/dev/can0"

int main(void)
{
    int fd;
    struct can_frame frame;
    uint8_t data[8];

    // 打开 CAN 设备
    fd = open(CAN_DEVICE, O_RDWR);
    if (fd == -1) {
        perror("Error opening CAN device");
        return 1;
    }

    // 设置 CAN 通信模式
    struct can_filter filter;
    memset(&filter, 0, sizeof(filter));
    filter.can_id = 0x12345678; // 使用的是一个固定的 CAN ID
    filter.can_mask = 0x00000000;
    if (ioctl(fd, CAN_SET_FILTER, &filter) < 0) {
        perror("Error setting CAN filter");
        close(fd);
        return 1;
    }

    // 发送 CAN 数据
    data[0] = 0x01; // 使用的是一个固定的数据 Byte
    data[1] = 0x23;
    data[2] = 0x45;
    data[3] = 0x67;
    data[4] = 0x89;
    data[5] = 0x90;
    data[6] = 0x91;
    data[7] = 0x92;
    data[8] = 0x93;
    frame.can_id = 0x12345678;
    frame.can_dlc = 8;
    //frame.data = data;
    memcpy(frame.data, data, sizeof(frame.data));

    if (write(fd, &frame, sizeof(frame)) != sizeof(frame)) {
        perror("Error sending CAN data");
        close(fd);
        return 1;
    }

    // 接收 CAN 数据
    uint8_t rx_data[8];
    frame.can_id = 0x12345678;
    frame.can_dlc = 8;
    //frame.data = rx_data;
    memcpy(rx_data, frame.data, sizeof(frame.data));
    if (read(fd, &frame, sizeof(frame)) != sizeof(frame)) {
        perror("Error receiving CAN data");
        close(fd);
        return 1;
    }

    // 打印接收到的数据
    printf("Received data: ");
    for (int i = 0; i < 8; i++) {
        printf("%02x ", rx_data[i]);
    }
    printf("\n");

    // 关闭 CAN 设备
    close(fd);

    return 0;
}
#endif
#if 0
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>

#define CAN_DEVICE "/dev/can0"

#define CAN_SET_FILTER CAN_RAW_SET_FILTER

int main(void)
{
    int fd;
    struct can_frame frame;
    uint8_t data[8];
    uint8_t rx_data[8];

    fd = open(CAN_DEVICE, O_RDWR);
    if (fd == -1) {
        perror("Error opening CAN device");
        return 1;
    }

    struct can_filter filter;
    memset(&filter, 0, sizeof(filter));
    filter.can_id = 0x12345678;
    filter.can_mask = 0x00000000;
    if (ioctl(fd, CAN_SET_FILTER, &filter) < 0) {
        perror("Error setting CAN filter");
        close(fd);
        return 1;
    }

    data[0] = 0x01;
    data[1] = 0x23;
    data[2] = 0x45;
    data[3] = 0x67;
    data[4] = 0x89;
    data[5] = 0x90;
    data[6] = 0x91;
    data[7] = 0x92;
    frame.can_id = 0x12345678;
    frame.can_dlc = 8;
    memcpy(frame.data, data, sizeof(frame.data));
    if (write(fd, &frame, sizeof(frame)) != sizeof(frame)) {
        perror("Error sending CAN data");
        close(fd);
        return 1;
    }

    if (read(fd, &frame, sizeof(frame)) != sizeof(frame)) {
        perror("Error receiving CAN data");
        close(fd);
        return 1;
    }

    memcpy(rx_data, frame.data, sizeof(frame.data));

    printf("Received data: ");
    for (int i = 0; i < 8; i++) {
        printf("%02x ", rx_data[i]);
    }
    printf("\n");

    close(fd);

    return 0;
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/can.h>
#include <linux/can/raw.h>

int main() {
    int s;
    struct sockaddr_can addr;
    struct can_frame frame;
    struct ifreq ifr;
    struct can_filter rfilter[1];

    // 创建套接字
    s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
    if (s < 0) {
        perror("Socket");
        return 1;
    }

    strcpy(ifr.ifr_name, "can0" );
    ioctl(s, SIOCGIFINDEX, &ifr);

    addr.can_family = AF_CAN;
    addr.can_ifindex = ifr.ifr_ifindex;

    // 将套接字绑定到CAN接口
    if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Bind");
        return 1;
    }

    // 设置过滤器
    rfilter[0].can_id = 0x123; // 只接收ID为0x123的帧
    rfilter[0].can_mask = CAN_SFF_MASK; // 标准帧格式掩码

    setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

    // 以下是接收或发送CAN帧的代码...

    close(s);
    return 0;
}

